﻿#include "DeviceParamDlg.h"
#include "ui_deviceParamdlg.h"
#include <QByteArray>


DeviceParamDlg::DeviceParamDlg(QWidget *parent) : QDialog(parent), ui(new Ui::DeviceParamDlg)
{
    ui->setupUi(this);
	
	setFixedSize(598, 488);
	setWindowTitle(QString::fromLocal8Bit("从模板添加终端参数"));
	ui->pAfirmPushBtn->setText(QString::fromLocal8Bit("确 定"));
	ui->pCacellPushBtn->setText(QString::fromLocal8Bit("取 消"));
	ui->pDevModelEdt->setReadOnly(true);
	ui->pYxNumEdit->setReadOnly(true);
	ui->pYcNumEdit->setReadOnly(true);
	ui->pYkNumEdit->setReadOnly(true);
	ui->pYmNumEdit->setReadOnly(true);
	ui->pBhycNumEdit->setReadOnly(true);
	ui->pParaNumEdit->setReadOnly(true);
	ui->pPrecNumEdit->setReadOnly(true);
	ui->pRybNumEdit->setReadOnly(true);
	ui->checkBox_Clear->setCheckState(Qt::Checked);
	m_DevModelId = 0;
	m_YxNum = 0;
	m_YcNum = 0;
	m_YkNum = 0;
	m_YmNum = 0;
	m_SetNum = 0;
	m_BhycNum = 0;
	m_PrecNum = 0;
	m_RybNum = 0;
	m_SetAreaNum=0;

	InitListShow();
	GetDeviceModelInfo();
	connect(ui->pBtn_ClearParam, SIGNAL(clicked()), this, SLOT(ClearDeviceParam()));
	connect(ui->pAfirmPushBtn, SIGNAL(clicked()), this, SLOT(CheckDeviceParam()));
	connect(ui->pCacellPushBtn, SIGNAL(clicked()), this, SLOT(close()));

	setModal(true);
}

DeviceParamDlg::~DeviceParamDlg()
{
	m_ColInfo.ReleasColumnInfo();
    delete ui;
}

void DeviceParamDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//	初始化设备模板列表显示
void DeviceParamDlg::InitListShow()
{
	ui->pDevModelLst->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui->pDevModelLst->setLineWidth(24);
	if (!ui->pDevModelLst->alternatingRowColors())
	{
		ui->pDevModelLst->setAlternatingRowColors(true);
	}
	ui->pDevModelLst->setEditTriggers(QAbstractItemView::NoEditTriggers);
	connect(ui->pDevModelLst, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(DevModelClicked(QListWidgetItem*)));
}

//	获取设备模板信息
int DeviceParamDlg::GetDeviceModelInfo()
{
	char *pBuff = NULL;
	int	Buff_len = 0;
	int	Ret = 0;
	unsigned int	ItemData =0;
	QString ItemText;
	QListWidgetItem *pListItem = NULL;
	ui->pDevModelLst->clear();
	QSize size(200, 19);
	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, pBuff, &Buff_len, false);
	if (Ret>0 && pBuff && Buff_len>0)
	{
		for (int i=0; i<Ret; i++)
		{
			ItemData = ((rDevModel_Info *)pBuff)[i].ID;
			pListItem = new QListWidgetItem(ui->pDevModelLst);
			ItemText = QString::fromLocal8Bit(((rDevModel_Info *)pBuff)[i].Name);
			pListItem->setText(ItemText);
			pListItem->setData(Qt::UserRole, ItemData);
			pListItem->setSizeHint(size);
			ui->pDevModelLst->addItem(pListItem);
		}
	}
	if (pBuff)
	{
		delete [](unsigned char*)pBuff;
		pBuff = NULL;
	}
	return 1;
}

void DeviceParamDlg::DevModelClicked(QListWidgetItem *pItem)
{
	if (!pItem)
	{
		return;
	}
	unsigned int	DevModelId = 0;
	DevModelId = pItem->data(Qt::UserRole).toUInt();
	if (m_DevModelId != DevModelId)
	{
		m_DevModelId = DevModelId;
	}
	ui->pDevModelEdt->setText("");
	if (m_DevModelId>0)
	{
		ui->pDevModelEdt->setText(pItem->text());
	}
	InitDevModeInfoShow();
}

//	显示设备模板信息
void DeviceParamDlg::InitDevModeInfoShow()
{
	m_YxNum = 0;
	m_YcNum = 0;
	m_YkNum = 0;
	m_YmNum = 0;
	m_SetNum = 0;
	m_BhycNum = 0;
	m_ParaNum = 0;
	m_PrecNum = 0;
	m_RybNum = 0;
	m_SetAreaNum=0;
	if (m_DevModelId > 0)
	{
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX,   m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_YxNum  );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC,   m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_YcNum  );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK,   m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_YkNum  );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM,   m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_YmNum  );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET,  m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_SetNum );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_BHYC, m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_BhycNum);
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PARA, m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_ParaNum);
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC, m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_PrecNum);
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RYB,  m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_RybNum );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SETAREA,  m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_SetAreaNum );
	}
	ui->pYxNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_YxNum));
	ui->pYcNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_YcNum));
	ui->pYkNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_YkNum));
	ui->pYmNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_YmNum));
	ui->pSetNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_SetNum));
	ui->pBhycNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_BhycNum));
	ui->pParaNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ParaNum));
	ui->pPrecNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_PrecNum));
	ui->pRybNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_RybNum));
}

//	判断设备参数
void DeviceParamDlg::CheckDeviceParam()
{
	int result = 1;
	if (Qt::Checked == ui->checkBox_Clear->checkState())
	{
		result = ClearDeviceParam();
	}
	if (result>0)
	{
		setCursor(QCursor(Qt::WaitCursor));
		InsertDeviceParam();
		setCursor(QCursor(Qt::ArrowCursor));
		close();
	}

	
}

int DeviceParamDlg::ClearDeviceParam()
{

	char	TableName[TABLE_ENG_NAME_LEN];
	int	Result = 0;
	
	memset(TableName, 0, TABLE_ENG_NAME_LEN);

	
	sprintf(TableName, "rParamS_Yx");
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YX, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("遥信删除失败"));
		return Result;
	}
	
	sprintf(TableName, "rParamS_Yc");
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YC, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("遥测删除失败"));
		return Result;
	}

	sprintf(TableName, "rParamS_Yk");
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YK, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("遥控删除失败"));
		return Result;
	}
	
	sprintf(TableName, "rParamS_Ym");
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YM, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("遥脉删除失败"));
		return Result;
	}
	
	sprintf(TableName, "rParamS_BhYc");
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_BHYC,TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("保护遥测删除失败"));
		return Result;
	}
	
	sprintf(TableName, "rParamS_Set"); 
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SET, TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("保护定值删除失败"));
		return Result;
	}
	sprintf(TableName, "rParamS_Dev"); 
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_DEV,  TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("装置参数删除失败"));
		return Result;
	}

	sprintf(TableName, "rParamS_JDXS");  
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_JDXS,  TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("精度系数删除失败"));
		return Result;
	}
	sprintf(TableName, "rParamS_Ryb");
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_RYB,  TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("软压板删除失败"));
		return Result;
	}
	sprintf(TableName, "rParamS_SetArea"); 
	Result = g_DbCfgFunObj.DeleteAllRecord(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SETAREA,  TableName);
	if(Result<0)
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("定值区删除失败"));
		return Result;
	}

	return 1;
}

//	插入设备
int DeviceParamDlg::InsertDeviceParam()
{
	int	Ret = 0;
	
	int newYxNum = 0;
	int newYcNum = 0;
	int newYkNum = 0;
	int newYmNum = 0;
	int newRybNum = 0; 
	int newBhycNum = 0;
	int newSetNum = 0;
	int newParaNum = 0;
	int newPrecNum = 0;
	int newSetAreaNum = 0;
	g_pSqLiteDB.BeginTransaction();
	newYxNum = InsertYx_New(m_DevModelId);
	newYcNum =	InsertYc_New(m_DevModelId);
	newYkNum	= InsertYk_New( m_DevModelId);
	newYmNum	= InsertYm_New(m_DevModelId);
	newRybNum	= InsertRyb(m_DevModelId);
	newBhycNum	= InsertBhYc(m_DevModelId);
	newSetNum  =  InsertSet(m_DevModelId);
	newParaNum  =  InsertParam( m_DevModelId);
	newPrecNum  =  InsertPrec(m_DevModelId);
	newSetAreaNum  =  InsertSetArea(m_DevModelId);
	g_pSqLiteDB.CommitTransaction();

	QString Str = QString::fromLocal8Bit("插入终端参数:\r\nYxNum:%1,	YcNum:%2\r\n\
YkNum:%3,	YmNum:%4\r\n\
RybNum:%5,	BhycNum:%6\r\n\
SetNum:%7,	ParaNum:%8\r\n\
PrecNum:%9  SetAreaNum%10\r\n")
		.arg(newYxNum)
		.arg(newYcNum)
		.arg(newYkNum)
		.arg(newYmNum)
		.arg(newRybNum)
		.arg(newBhycNum)
		.arg(newSetNum)
		.arg(newParaNum)
		.arg(newPrecNum)
		.arg(newSetAreaNum);

	QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), Str, QMessageBox::Ok | QMessageBox::Cancel);

	return 1;
}


//	插入遥信
int  DeviceParamDlg::InsertYx_New( unsigned int ModelId)
{
	if (m_YxNum<=0)
	{
		return 0;
	}
	if ( ModelId<=0)
	{
		return -1;
	}

	int			Ret = 0;
	int			Result = 0;
	rParamS_Yx	rYx_Info;
	int			TotalYxNum = 0;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			YxName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
//	bool			bFirstRowFlag = true;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_Yx");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YX);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	//g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YX, RecStartId);
	g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YX, tableName, InsertHeadSqlStr);

	if (m_YxNum>0)
	{
		memset(m_sql, 0, SQL_MAX_LEN);
		sprintf(m_sql, "select * from rDevModel_Yx where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YX, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			if ((PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YX, tableName, InsertHeadSqlStr);

//			bFirstRowFlag = true;

			SqlStr.clear();
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYx_Info, 0, sizeof(rParamS_Yx));
				YxName.clear();
				g_DbCfgFunObj.SetDefaultData((char*)&rYx_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength,0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YX, (char*)&rYx_Info, TotalYxNum);

				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YX, RecStartId);
				rYx_Info.ID = RecStartId;
				rYx_Info.IndexNo = TotalYxNum+1;
				rYx_Info.m_fun = ((rDevModel_Yx*)pBuff)[i].Fun;
				rYx_Info.m_inf = ((rDevModel_Yx*)pBuff)[i].Inf;
				rYx_Info.m_yx_type = ((rDevModel_Yx*)pBuff)[i].YxType;
				
				YxName = QString::fromLocal8Bit(((rDevModel_Yx*)pBuff)[i].Name);

				g_DbCfgFunObj.InputStrToBuff(rYx_Info.Name, YxName, NAME_SIZE);
				g_DbCfgFunObj.GetValuesSql((char*)&rYx_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

			//	if ((SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length()) >= (SQL_MAX_LEN*2) )
			//	{
					SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥信记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
//					bFirstRowFlag = true;
//				}
// 				if (bFirstRowFlag)
// 				{
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 
// 				SqlStr += InsertValSqlStr;
				TotalYxNum++;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥信记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}

	}
	m_ColInfo.ReleasColumnInfo();
	return TotalYxNum;
}


//	插入遥测
int DeviceParamDlg::InsertYc_New(unsigned int ModelId)
{
	if (m_YcNum<=0)
	{
		return 0;
	}

	if (ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rParamS_Yc		rYc_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			YcName;
	QString			DataSetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
	bool			bFirstRowFlag = true;
	int				length = 0;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_Yc");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YC);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_YcNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_Yc where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YC, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YC, RecStartId);
// 			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YC, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYc_Info, 0, sizeof(rParamS_Yc));
				YcName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rYc_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YC, (char*)&rYc_Info, i);
				
				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YC, RecStartId);
				rYc_Info.ID = RecStartId;
				rYc_Info.IndexNo = i+1;
				rYc_Info.m_fun = ((rDevModel_Yc*)pBuff)[i].Fun;
				rYc_Info.m_inf = ((rDevModel_Yc*)pBuff)[i].Inf;
				rYc_Info.percent = ((rDevModel_Yc*)pBuff)[i].percent;			
				rYc_Info.rating = ((rDevModel_Yc*)pBuff)[i].rating;			
				rYc_Info.ratio = ((rDevModel_Yc*)pBuff)[i].ratio;		
				rYc_Info.offset = ((rDevModel_Yc*)pBuff)[i].offset;	
				rYc_Info.High_limit = ((rDevModel_Yc*)pBuff)[i].High_limit;
				rYc_Info.Low_limit = ((rDevModel_Yc*)pBuff)[i].Low_limit;
				rYc_Info.tYCLimit = ((rDevModel_Yc*)pBuff)[i].tYCLimit;
				memcpy(rYc_Info.unit, ((rDevModel_Yc*)pBuff)[i].unit, UNIT_NAME_LEN);

				YcName = QString::fromLocal8Bit(((rDevModel_Yc*)pBuff)[i].Name);
				
				g_DbCfgFunObj.InputStrToBuff(rYc_Info.Name, YcName, NAME_SIZE);
				g_DbCfgFunObj.GetValuesSql((char*)&rYc_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

// 				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
// 				{
					SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = true;
// 				}
// 				if (bFirstRowFlag)
// 				{
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 				SqlStr += InsertValSqlStr;

			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}
		m_ColInfo.ReleasColumnInfo();
		return m_YcNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;
}

//	插入遥信
int  DeviceParamDlg::InsertRyb(unsigned int ModelId)
{
	if (m_RybNum<=0)
	{
		return 0;
	}
	if (ModelId<=0 )
	{
		return -1;
	}

	int			Ret = 0;
	int			Result = 0;
	rParamS_Ryb	ryb_Info;
	int			TotalRybNum = 0;
	char			*pBuff = NULL;
	int			buff_len = 0;
	int			i=0;
	unsigned int	RecStartId = 0;
	QString			YxName;
	QString			DataSetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
//	bool			bFirstRowFlag = true;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_Ryb");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_RYB);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

//	g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_RYB, RecStartId);
	g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_RYB, tableName, InsertHeadSqlStr);

	if (m_RybNum>0)
	{
		memset(m_sql, 0, SQL_MAX_LEN);
		sprintf(m_sql, "select * from rDevModel_Ryb where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_RYB, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			if ((PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_RYB, tableName, InsertHeadSqlStr);

//			bFirstRowFlag = true;
//			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{	
				memset((char*)&ryb_Info, 0, sizeof(rParamS_Ryb));
				YxName.clear();
				g_DbCfgFunObj.SetDefaultData((char*)&ryb_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_RYB, (char*)&ryb_Info, TotalRybNum);

				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_RYB, RecStartId);
				ryb_Info.ID       = RecStartId;
				ryb_Info.IndexNo  = TotalRybNum+1;
				ryb_Info.m_fun	  = ((rDevModel_Ryb*)pBuff)[i].Fun;
				ryb_Info.m_inf	  = ((rDevModel_Ryb*)pBuff)[i].Inf;
		
				YxName = QString::fromLocal8Bit(((rDevModel_Ryb*)pBuff)[i].Name);
				
				g_DbCfgFunObj.InputStrToBuff(ryb_Info.Name, YxName, NAME_SIZE);
				g_DbCfgFunObj.GetValuesSql((char*)&ryb_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}
				
// 				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
// 				{
					SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入软压板记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = true;
// 				}
// 				if (bFirstRowFlag)
// 				{
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 				SqlStr += InsertValSqlStr;
				TotalRybNum++;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入软压板记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}

	}
	m_ColInfo.ReleasColumnInfo();
	return TotalRybNum;
}



//插入遥控
int DeviceParamDlg::InsertYk_New(unsigned int ModelId)
{
	if (m_YkNum<=0)
	{
		return 0;
	}

	if (ModelId<=0 )
	{
		return -1;
	}

	int			Ret = 0;
	int			Result = 0;
	rParamS_Yk	rYk_Info;
	char			*pBuff = NULL;
	int			buff_len = 0;
	int			i=0;
	unsigned int	RecStartId = 0;
	QString			YkName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
//	bool			bFirstRowFlag = true;

	char			tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_Yk");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YK);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_YkNum>0 )
	{
		sprintf(m_sql, "select * from rDevModel_Yk where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YK, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YK, RecStartId);
// 			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YK, tableName, InsertHeadSqlStr);

// 			bFirstRowFlag = true;
// 			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYk_Info, 0, sizeof(rParamS_Yk));
				YkName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rYk_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YK, (char*)&rYk_Info, i);
				
				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YK, RecStartId);
				rYk_Info.ID = RecStartId;
				rYk_Info.IndexNo = i+1;
				rYk_Info.m_fun = ((rDevModel_Yk*)pBuff)[i].Fun;
				rYk_Info.m_inf = ((rDevModel_Yk*)pBuff)[i].Inf;
					
				YkName = QString::fromLocal8Bit(((rDevModel_Yk*)pBuff)[i].Name);
				
				g_DbCfgFunObj.InputStrToBuff(rYk_Info.Name, YkName, NAME_SIZE);

				g_DbCfgFunObj.GetValuesSql((char*)&rYk_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

// 				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
// 				{
					SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥控记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = true;
// 				}
// 				if (bFirstRowFlag)
// 				{
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥控记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}
	}
	m_ColInfo.ReleasColumnInfo();
	return m_YkNum;
}


//	插入遥脉
int DeviceParamDlg::InsertYm_New(unsigned int ModelId)
{
	if (m_YmNum<=0)
	{
		return 0;
	}
	if ( ModelId<=0)
	{
		return -1;
	}

	int			Ret = 0;
	int			Result = 0;
	rParamS_Ym	rYm_Info;
	char			*pBuff = NULL;
	int			buff_len = 0;
	int			i=0;
	
	unsigned int	RecStartId = 0;
	QString			YmName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
//	bool			bFirstRowFlag = true;
	char			tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_Ym");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YM);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_YmNum>0 )
	{
		sprintf(m_sql, "select * from rDevModel_Ym where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YM, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YM, RecStartId);
// 			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YM, tableName, InsertHeadSqlStr);

// 			bFirstRowFlag = true;
// 			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYm_Info, 0, sizeof(rParamS_Ym));
				YmName.clear();
				g_DbCfgFunObj.SetDefaultData((char*)&rYm_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YM, (char*)&rYm_Info, i);
			
				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_YM, RecStartId);
				rYm_Info.ID = RecStartId;
				rYm_Info.IndexNo = i+1;

				rYm_Info.m_fun = ((rDevModel_Ym*)pBuff)[i].Fun;
				rYm_Info.m_inf = ((rDevModel_Ym*)pBuff)[i].Inf;
			
				YmName = QString::fromLocal8Bit(((rDevModel_Ym*)pBuff)[i].Name);
			
				g_DbCfgFunObj.InputStrToBuff(rYm_Info.Name, YmName, NAME_SIZE);
				
				g_DbCfgFunObj.GetValuesSql((char*)&rYm_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

// 				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
// 				{
					SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥脉记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = true;
// 				}
// 				if (bFirstRowFlag)
// 				{
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥脉记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}
	}
	m_ColInfo.ReleasColumnInfo();
	return m_YmNum;
	return 0;
}

//	插入遥测
int DeviceParamDlg::InsertBhYc(unsigned int ModelId)
{
	if (m_BhycNum<=0)
	{
		return 0;
	}

	if (ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rParamS_BhYc		rbhYc_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			YcName;
	QString			DataSetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
// 	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_BhYc");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_BHYC);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_BhycNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_Bhyc where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_BHYC, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_BHYC, RecStartId);
// 			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_BHYC, tableName, InsertHeadSqlStr);

// 			bFirstRowFlag = true;
// 			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rbhYc_Info, 0, sizeof(rParamS_BhYc));
				YcName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rbhYc_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_BHYC, (char*)&rbhYc_Info, i);

				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_BHYC, RecStartId);
				rbhYc_Info.ID		  = RecStartId;
				rbhYc_Info.IndexNo  = i+1;
				rbhYc_Info.m_fun	  = ((rDevModel_Bhyc*)pBuff)[i].Fun;
				rbhYc_Info.m_inf	  = ((rDevModel_Bhyc*)pBuff)[i].Inf;

				rbhYc_Info.percent = ((rDevModel_Bhyc*)pBuff)[i].percent;			
				rbhYc_Info.rating = ((rDevModel_Bhyc*)pBuff)[i].rating;			
				rbhYc_Info.ratio = ((rDevModel_Bhyc*)pBuff)[i].ratio;		
				rbhYc_Info.offset = ((rDevModel_Bhyc*)pBuff)[i].offset;	
				rbhYc_Info.High_limit = ((rDevModel_Bhyc*)pBuff)[i].High_limit;
				rbhYc_Info.Low_limit = ((rDevModel_Bhyc*)pBuff)[i].Low_limit;
				rbhYc_Info.tYCLimit = ((rDevModel_Bhyc*)pBuff)[i].tYCLimit;
				memcpy(rbhYc_Info.unit, ((rDevModel_Bhyc*)pBuff)[i].unit, UNIT_NAME_LEN);
				
					YcName = QString::fromLocal8Bit(((rDevModel_Bhyc*)pBuff)[i].Name);
				
				g_DbCfgFunObj.InputStrToBuff(rbhYc_Info.Name, YcName, NAME_SIZE);
				g_DbCfgFunObj.GetValuesSql((char*)&rbhYc_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

// 				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
// 				{
					SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入保护遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = true;
// 				}
// 				if (bFirstRowFlag)
// 				{
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入保护遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}
// 		m_ColInfo.ReleasColumnInfo();
		return m_BhycNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;
}

int DeviceParamDlg::InsertSet(unsigned int ModelId)
{
	if (m_SetNum<=0)
	{
		return 0;
	}

	if (ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rParamS_Set		rSet_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			SetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
// 	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_Set");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SET);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_SetNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_Set where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_SET, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SET, RecStartId);
// 			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SET, tableName, InsertHeadSqlStr);

// 			bFirstRowFlag = true;
// 			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rSet_Info, 0, sizeof(rParamS_Set));
				SetName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rSet_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SET, (char*)&rSet_Info, i);

				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SET, RecStartId);
				rSet_Info.ID		  = RecStartId;
				rSet_Info.IndexNo  = ((rDevModel_Set*)pBuff)[i].IndexNo;
				rSet_Info.m_fun	  = ((rDevModel_Set*)pBuff)[i].Fun;
				rSet_Info.m_inf	  = ((rDevModel_Set*)pBuff)[i].Inf;
				rSet_Info.daType = ((rDevModel_Set*)pBuff)[i].daType;			
				rSet_Info.daFormat = ((rDevModel_Set*)pBuff)[i].daFormat;			
				rSet_Info.defval = ((rDevModel_Set*)pBuff)[i].defval;		
				rSet_Info.min_val = ((rDevModel_Set*)pBuff)[i].min_val;	
				rSet_Info.max_val = ((rDevModel_Set*)pBuff)[i].max_val;
				rSet_Info.step_val = ((rDevModel_Set*)pBuff)[i].step_val;
				memcpy(rSet_Info.unit, ((rDevModel_Set*)pBuff)[i].unit, UNIT_NAME_LEN);
				
				memcpy(rSet_Info.Name, ((rDevModel_Set*)pBuff)[i].Name, NAME_SIZE);
					
				g_DbCfgFunObj.GetValuesSql((char*)&rSet_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

// 				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
// 				{
				SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入保护遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = true;
// 				}
// 				if (bFirstRowFlag)
// 				{
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入保护定值记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}
		m_ColInfo.ReleasColumnInfo();
		return m_SetNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;
}

int	DeviceParamDlg::InsertParam( unsigned int ModelId)
{
	if (m_ParaNum<=0)
	{
		return 0;
	}

	if (ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rParamS_Dev		rDevPara_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			SetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
//	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_Dev");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_DEV);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_SetNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_Para where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_PARA, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_DEV, RecStartId);
// 			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_DEV, tableName, InsertHeadSqlStr);

// 			bFirstRowFlag = true;
// 			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rDevPara_Info, 0, sizeof(rDevS_Param));
				SetName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rDevPara_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_PARAM, (char*)&rDevPara_Info, i);

				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_DEV, RecStartId);
				rDevPara_Info.ID		  = RecStartId;
				rDevPara_Info.IndexNo  = ((rDevModel_Para*)pBuff)[i].IndexNo;
				rDevPara_Info.m_fun	  = ((rDevModel_Para*)pBuff)[i].Fun;
				rDevPara_Info.m_inf	  = ((rDevModel_Para*)pBuff)[i].Inf;
				rDevPara_Info.daType = ((rDevModel_Para*)pBuff)[i].daType;			
				rDevPara_Info.daFormat = ((rDevModel_Para*)pBuff)[i].daFormat;			
				rDevPara_Info.defval = ((rDevModel_Para*)pBuff)[i].defval;		
				rDevPara_Info.min_val = ((rDevModel_Para*)pBuff)[i].min_val;	
				rDevPara_Info.max_val = ((rDevModel_Para*)pBuff)[i].max_val;
				rDevPara_Info.step_val = ((rDevModel_Para*)pBuff)[i].step_val;
				memcpy(rDevPara_Info.unit, ((rDevModel_Para*)pBuff)[i].unit, UNIT_NAME_LEN);

				memcpy(rDevPara_Info.Name, ((rDevModel_Para*)pBuff)[i].Name, NAME_SIZE);

				g_DbCfgFunObj.GetValuesSql((char*)&rDevPara_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

// 				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
// 				{
				SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入参数记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = true;
// 				}
// 				if (bFirstRowFlag)
// 				{
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入参数记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}
		m_ColInfo.ReleasColumnInfo();
		return m_ParaNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;

}


int	DeviceParamDlg::InsertPrec( unsigned int ModelId)
{
	if (m_PrecNum<=0)
	{
		return 0;
	}

	if (ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rParamS_JDXS		rJDxss_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			PrecName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
// 	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_JDXS");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_JDXS);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_SetNum>0 )
	{
		
		sprintf(m_Dsql, "select * from rDevModel_Prec where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_PREC, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_JDXS, RecStartId);
// 			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_JDXS, tableName, InsertHeadSqlStr);

// 			bFirstRowFlag = true;
// 			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rJDxss_Info, 0, sizeof(rParamS_JDXS));
				PrecName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rJDxss_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_JDXS, (char*)&rJDxss_Info, i);

				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_JDXS, RecStartId);
				rJDxss_Info.ID		  = RecStartId;
				rJDxss_Info.IndexNo  = ((rDevModel_Prec*)pBuff)[i].IndexNo;
				//rJDxss_Info.m_fun	  = ((rDevModel_Prec*)pBuff)[i].Fun;
				//rJDxss_Info.m_inf	  = ((rDevModel_Prec*)pBuff)[i].Inf;			
				rJDxss_Info.defval = ((rDevModel_Prec*)pBuff)[i].defval;		
				rJDxss_Info.min_val = ((rDevModel_Prec*)pBuff)[i].min_val;	
				rJDxss_Info.max_val = ((rDevModel_Prec*)pBuff)[i].max_val;
				memcpy(rJDxss_Info.Name, ((rDevModel_Prec*)pBuff)[i].Name, NAME_SIZE);

				g_DbCfgFunObj.GetValuesSql((char*)&rJDxss_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

// 				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
// 				{
				SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入精度记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = true;
// 				}
// 				if (bFirstRowFlag)
// 				{
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入精度记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}
		m_ColInfo.ReleasColumnInfo();
		return m_PrecNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;

}

int	DeviceParamDlg::InsertSetArea( unsigned int ModelId)
{
	if (m_SetAreaNum<=0)
	{
		return 0;
	}

	if (ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rParamS_SetArea	rArea_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			PrecName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
//	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rParamS_SetArea");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SETAREA);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_SetAreaNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_SetArea where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_SETAREA, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SETAREA, RecStartId);
// 			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SETAREA, tableName, InsertHeadSqlStr);

// 			bFirstRowFlag = true;
// 			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rArea_Info, 0, sizeof(rParamS_SetArea));
				PrecName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rArea_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SETAREA, (char*)&rArea_Info, i);

				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RPARAMS_SETAREA, RecStartId);
				rArea_Info.ID		  = RecStartId;
				rArea_Info.IndexNo  = ((rDevModel_SetArea*)pBuff)[i].IndexNo;
				rArea_Info.m_fun	  = ((rDevModel_SetArea*)pBuff)[i].Fun;
				rArea_Info.m_inf	  = ((rDevModel_SetArea*)pBuff)[i].Inf;			
				rArea_Info.value = ((rDevModel_SetArea*)pBuff)[i].value;		
				memcpy(rArea_Info.Name, ((rDevModel_SetArea*)pBuff)[i].Name, NAME_SIZE);

				g_DbCfgFunObj.GetValuesSql((char*)&rArea_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

// 				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
// 				{
					SqlStr = InsertHeadSqlStr+InsertValSqlStr;
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入定值区记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
// 					SqlStr = InsertHeadSqlStr;
// 					bFirstRowFlag = true;
// 				}
// 				if (bFirstRowFlag)
// 				{
// 					bFirstRowFlag = false;
// 				}else
// 				{
// 					SqlStr += QString::fromLocal8Bit(",");
// 				}
// 				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
// 		if (SqlStr.length()>0)
// 		{
// 			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
// 			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
// 			if (Result<0)
// 			{
// 				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入定值区记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
// 				m_ColInfo.ReleasColumnInfo();
// 				return -5;
// 			}
// 			SqlStr.clear();
// 		}
		m_ColInfo.ReleasColumnInfo();
		return m_SetAreaNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;

}